<template>
  <div class="swAuTvmShort">
    <basic-container>
      <avue-crud
        ref="crud"
        :option="option"
        :table-loading="listLoading"
        :data="list"
        :page.sync="page"
        @search-change="handleFilter"
        @selection-change="selectionChange"
        @refresh-change="refreshChange"
        @on-load="refreshChange">
        <template slot-scope="scope" slot="lineCodeSearch">
          <el-select v-model="searchForm.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" filterable
                     clearable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCodeSearch">
          <el-select v-model="searchForm.stationCode" placeholder="请选择" style="width: 150px" clearable filterable
                     @change="changeStation">
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="conductorPersonNameSearch">
          <el-select v-model="searchForm.conductorPersonName" placeholder="请选择" style="width: 150px" clearable
                     filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.nikename"
              :value="item.nikename">
            </el-option>
          </el-select>
        </template>
        <template slot="searchMenu" slot-scope="{row,size}">
          <el-button :size="size" @click="()=>{setSearchFiledsEmpty(row);setSearchFiledsEmpty(searchForm);getList()}" icon="el-icon-delete">清空</el-button>
        </template>
        <template slot-scope="scope" slot="menuRight">
          <el-button class="filter-item" type="primary" size="mini" @click="supPaymentTimelimit(1)">设置补款期限</el-button>
          <el-button class="filter-item" type="primary" size="mini" @click="supPaymentTimelimit(2)">设置入账日期</el-button>
          <el-button class="filter-item" type="primary" size="mini" @click="isNotice()">通知</el-button>
<!--          <el-button type="primary"  size="mini" @click="exportData">导出</el-button>-->
        </template>
      </avue-crud>
    </basic-container>
    <el-dialog
      title="设置补款期限"
      :visible.sync="supPaymentTimelimitDialogVisible"
      width="1500px" >
      <avue-crud
        ref="crud"
        :option="optionLimit"
        :table-loading="listLoadingLimit"
        :data="listLimit"
        @on-load="getLimitList">
        <template slot="supPaymentLimit" slot-scope="{row}">
          <el-input
            v-model="row.supPaymentLimit"
            placeholder="请输入天数"></el-input>
        </template>
        <template slot="accountTime" slot-scope="{row}">
          <el-date-picker
            v-model="row.accountTime"
            type="date"
            placeholder="选择日期">
          </el-date-picker>
        </template>
      </avue-crud>
      <template slot="footer">
        <center>
          <el-button type="primary" size="small"
                     @click="submit">确认
          </el-button>
          <el-button size="small"
                     @click="supPaymentTimelimitDialogVisible=false">取消</el-button>
        </center>
      </template>
    </el-dialog>
  </div>
</template>

<script>
// import ElInput from "@/node_modules/element-ui/packages/input/src/input";
import {
  getSysUserToLine
} from '@/api/subwayRat/conductorDifferences/swAuConductorShort/swAuConductorShort';
import { setSearchFiledsEmpty,getLineAndStationByCode,getSysUserToLS,getgetStationCodes,
  getLineCodes,} from "@/api/subwayRat/commonCheck";
import {exportDataToExcel} from "@/api/subwayRat/exportSelf";
import {
  tableOption,
  tableOptionLimit,
  getSwAuTvmShort,
  getSwAuTvmShortLimit,
  updateSwAuTvmShortLimit,
  updateSwAuTvmShort
} from '@/api/subwayRat/swAuTvm/swAuTvmShort';
export default {
  components: {
    // ElInput
  },
  name: 'swAuTvmShort',
  data() {
    return {
      type : 0,
      userList:[],
      selectKeys:[], //存放对像
      selectKeyVals:[], //存放值
      option: tableOption,
      optionLimit: tableOptionLimit,
      listLoading: true,
      listLoadingLimit: true,
      supPaymentTimelimitDialogVisible: false,
      searchForm:{
        lineCode: '9',
        stationCode: '',
        conductorPersonName: '',
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 20, // 每页显示多少条,
        isAsc: false// 是否倒序
      },
      list : [],
      listLimit : [],
      lineCodes: [],
      stationCodes: [],
      conductorPersonNames: [],
    }
  },
  computed: {
  },
  watch: {
  },
  created() {
    this.changeLine("9")
  },
  mounted() {
    getSysUserToLine({}).then((response)=>{
      this.userList = response.data.data;
    });
    getLineCodes().then(res => { //获取线路
      this.lineCodes = res;
    });
  },
  methods: {
    setSearchFiledsEmpty,
    refreshChange() {
      this.getList()
    },
    changeLine(v) { //改变线路时
      getgetStationCodes(v).then(res => {
        this.stationCodes = res;
        this.searchForm.stationCode = '';
        this.searchForm.conductorPersonName = '';
      })
    },
    changeStation(v) {
      let p = {
        line_code : this.searchForm.lineCode,
        station_code : v
      };
      getSysUserToLS(p).then((response)=>{
        this.conductorPersonNames = response.data.data;
        this.searchForm.conductorPersonName = '';
      });
    },
    async submit(){
      let p = {
        list : JSON.stringify(this.listLimit)
      };
      await updateSwAuTvmShortLimit(p);
      this.supPaymentTimelimitDialogVisible = false;
      this.getList();
    },
    supPaymentTimelimit(type){
      if(this.selectKeyVals.length<=0){
        this.$message.warning("请至少选择一条数据操作！");
        return;
      }
      for(let i=0;i<this.selectKeys.length;i++){
        if(1 == type && 1 == this.selectKeys[i].isNotice){
          this.$message.warning("勾选数据中存在已通知数据！");
          return
        }else if (2 == type && 0 == this.selectKeys[i].isSupPayment){
          this.$message.warning("勾选数据中存在未补款数据！");
          return
        }
      }
      this.getLimitList();
      this.supPaymentTimelimitDialogVisible = true;
    },
    getLimitList() {
      this.listLoadingLimit = true;
      let p = {
        ids : JSON.stringify(this.selectKeyVals),
        userCode : this.$store.getters.userInfo.userCode
      };
      getSwAuTvmShortLimit(p).then((response)=>{
        this.listLimit = response.data.data
        this.listLoadingLimit = false
      });
    },
    async isNotice(){
      if(this.selectKeyVals.length<=0){
        this.$message.warning("请选择一条数据操作！");
        return;
      }
      let list = [];
      for(let i=0;i<this.selectKeys.length;i++){
        list.push({id:this.selectKeys[i].id,supPaymentLimit:this.selectKeys[i].supPaymentLimit});
      }
      let messageLSList = [];
      for(let i=0;i<this.selectKeys.length;i++){
        let data = await getLineAndStationByCode({"lineCode":this.selectKeys[i].lineCode, "code":this.selectKeys[i].stationCode});
        messageLSList.push({
          "content":"车站：" + data.data[0].name,
          "lineCode":this.selectKeys[i].lineCode,
          "stationCode":this.selectKeys[i].stationCode
        })
      }
      let p = {
        messageLSList:JSON.stringify(messageLSList),
        //title:this.$store.getters.tag.label,
        title: 'TVM票款短款补款通知',
        msgType:"通知",
        type:"swAuTvmShort_001",
        list : JSON.stringify(list)
      };
      updateSwAuTvmShort(p).then((res)=>{
        if(res.data.code==0){
          this.$message.success("操作成功！");
          this.getList();
        }else{
          this.$message.error("操作失败！");
        }
      });
    },
    async exportData(){ // 导出
      this.searchForm.pageNo=1;
      this.searchForm.pageSize=1000000000;
      if(this.selectKeys.length>0){
        let newData = JSON.parse(JSON.stringify(this.selectKeys));
        exportDataToExcel(newData,this.option.column,'TVM票款短款补款信息');
      }else {
        let res=await getSwAuConductorShort(this.searchForm);
        let data = res.data.data;
        exportDataToExcel(data,this.option.column,'TVM票款短款补款信息');
      }
    },
    getList() {
      this.listLoading = true;
      Object.assign(this.searchForm,this.page);
      this.searchForm.userCode = this.$store.getters.userInfo.userCode;
      getSwAuTvmShort(this.searchForm).then((response)=>{
        this.list = response.data.data
        this.listLoading = false
        this.page.total=response.data.total;
      });
    },
    handleFilter(params, done) {
      Object.assign(this.searchForm,params);
      this.getList()
      done()
    },
    selectionChange(list){
      this.selectKeys=list;
      this.selectKeyVals=[];
      for(let i=0;i<list.length;i++){
        this.selectKeyVals.push(list[i].id);
      }
    }
  }
}
</script>
<style lang="scss">
</style>
